package com.example.yinhangmiaosha.config;

import com.example.yinhangmiaosha.filter.MyFilter;
import com.example.yinhangmiaosha.filter.TimeFilter;
import com.example.yinhangmiaosha.realm.UserRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    /**
     * 自定义Realm
     */
    @Bean
    public UserRealm userRealm()
    {
        return new UserRealm();
    }
    /**
     * 安全管理器
     */
    @Bean
    public org.apache.shiro.mgt.SecurityManager securityManager(UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        securityManager.setSessionManager(sessionManager());
        return securityManager;
    }

    @Bean
    public DefaultWebSessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        // 去掉shiro登录时url里的JSESSIONID
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }
    /**
     * Shiro过滤器配置
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(org.apache.shiro.mgt.SecurityManager securityManager)
    {
        // Shiro的核心安全接口,这个属性是必须的
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 设置登录页面
//        shiroFilterFactoryBean.setLoginUrl("/login.html");
//        修改
        shiroFilterFactoryBean.setLoginUrl("/userLogin?username=admin&password=123");
        // 设置成功页面
//        shiroFilterFactoryBean.setSuccessUrl("/test.html");
        // 设置未授权接口
        shiroFilterFactoryBean.setUnauthorizedUrl("/nopermission");
        // Shiro连接约束配置，即过滤链的定义
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        // 对静态资源设置匿名访问
//        filterChainDefinitionMap.put("/**", "anon");
//        filterChainDefinitionMap.put("/favicon.ico**", "anon");
//        filterChainDefinitionMap.put("/logo.png**", "anon");
//        filterChainDefinitionMap.put("/html/**", "anon");
        // 不需要拦截的访问
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        filterChainDefinitionMap.put("/img/**", "anon");
        filterChainDefinitionMap.put("/login.html", "anon");
        filterChainDefinitionMap.put("/index.html", "anon");
        filterChainDefinitionMap.put("/userLogin", "anon");
        // 登录拦截（未登录则跳转至登录页面）
        // /**表拦截所有路径，而/*只拦截一层"/"
        filterChainDefinitionMap.put("/**", "authc");
        // 权限拦截（未授权将跳转至未授权接口）
        filterChainDefinitionMap.put("/**", "perms[user:permitted]");
        // 在套上自定义过滤器
        Map<String, Filter> filter = new LinkedHashMap<String, Filter>();
        filter.put("MyFilter", new MyFilter());
        filter.put("TimeFilter", new TimeFilter());
        shiroFilterFactoryBean.setFilters(filter);
        // 先通过自定义权限过滤器，再验证shiro权限过滤器
//        filterChainDefinitionMap.put("/**", "TimeFilter,perms[user:permitted],MyFilter");
//        filterChainDefinitionMap.put("/order/**", "TimeFilter,perms[user:permitted]");
        // 退出 logout地址，shiro去清除session
        filterChainDefinitionMap.put("/logout", "logout");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
        LogoutFilter logoutFilter = new LogoutFilter();
        logoutFilter.setRedirectUrl("/login.html");
        filters.put("logout", logoutFilter);

        return shiroFilterFactoryBean;
    }

    /**
     * shiro注解通知器
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    /**
     * CGlib代理
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }
}
